<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Crypt;
use DB;

class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add');
    }

    public function insert(Request $request)
    {
    	$this -> validate($request,[
    		'name' => 'required|min:2|max:18',
    		'password' => 'required',
    		'repassword' => 'required|same:password',
    		'email' => 'email',
    		'pic' => 'image',
    		],[
    		'name.required' => '用户名不能为空',
    		'name.min' => '用户名不能少于2个字符',
    		'name.max' => '用户名不能大于18个字符',
    		'password.required' => '密码不能为空',
    		'repassword.required' => '确认密码不能为空',
    		'repassword.same' => '确认密码不一致',
    		'email.email' => '邮箱格式不合法',
    		'pic.image' => '请上传一张图片',
    		]);
    	$data = $request -> except('_token','repassword');

    	//处理图片
    	if($request -> hasfile('pic'))
    	{
    		if($request -> file('pic')-> isValid());
    		{
    			//获取扩展名
    			$extension = $request -> file('pic') -> getClientOriginalExtension();
    			$filename = str_random(6).'.'.$extension;
    			$dir = './uploads/imgs';

    			if(!file_exists($dir))
    			{
    				mkdir($dir,0777,true);
    			}
    			//移动文件
    			$move = $request -> file('pic') -> move($dir, $filename);
    			if($move)
    			{
    				$data['pic'] = $filename;
    			}
    		}

    	}

    	//加密
    	$data['password'] = Crypt::encrypt($data['password']);
        // dd($data);
        
    	$res = DB::table('users') -> insert($data);
        
    	if($res)
    	{
    		return redirect('admin/user/index') -> with(['info'=>'添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }

    public function index(Request $request)
    {
        $num = $request -> input('num',10);
        $keyword = $request -> input('keyword','');
        $data = DB::table('users') ->where('name','like','%'.$keyword.'%') -> paginate($num);
    	return view('admin.user.index',['data' => $data,'request' => $request -> all()]);
    }

    public function changename(Request $request)
    {
       $data = $request -> all();

       $res = DB::table('users') -> where('name',$data['name']) -> first();

       if($res)
       {
            return response() -> json(0);
       }else
       {

            $r = DB::table('users') -> where('id',$data['id']) -> update(['name' => $data['name']]);
           
            if($r)
            {
                return response() -> json(1);
            }else
            {
                return response() -> json(2);
            }
       }
     }

     public function edit($id)
     {
        $data = DB::table('users') -> where('id',$id) -> first();
        return view('admin.user.edit',['data' => $data]);
     }

     public function update(Request $request, $id)
     {
        //验证
        $this -> validate($request,[
            'name' => 'required|min:2|max:18',
            'password' => 'required',
            'repassword' => 'required|same:password',
            'email' => 'email',
            'pic' => 'image',
            ],[
            'name.required' => '用户名不能为空',
            'name.min' => '用户名不能少于2个字符',
            'name.max' => '用户名不能大于18个字符',
            'password.required' => '密码不能为空',
            'repassword.required' => '确认密码不能为空',
            'repassword.same' => '确认密码不一致',
            'email.email' => '邮箱格式不合法',
            'pic.image' => '请上传一张图片',
            ]);

        $oldpic = DB::table('usres') -> where('id',$id) -> first() ->pic;
        $oldpath = '/uploads/imgs/'.$oldpic;
        //处理图片
        if($request -> hasfile('pic'))
        {
            if($request -> file('pic')-> isValid());
            {
                //获取扩展名
                $extension = $request -> file('pic') -> getClientOriginalExtension();
                $filename = str_random(6).'.'.$extension;
                $dir = './uploads/imgs';

                if(!file_exists($dir))
                {
                    mkdir($dir,0777,true);
                }
                //移动文件
                $move = $request -> file('pic') -> move($dir, $filename);
                if($move)
                {
                    $data['pic'] = $filename;
                }
            }

        }


        $data = $request -> except('_token','repassword');

        $res = DB::table('users') -> where('id',$id) -> update($data);

        if($res)
        {
            if($file_exists($oldpath))
            {
                unlink($oldpath);
            }
            return redirect('/admin/user/index') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info'=>'修改失败']);
        }
     }
}
